<?php
namespace app\api\controller;

use think\Db;
use think\Exception;

class Feedback extends BaseController
{
    /**
     * 提交反馈
     */
    public function submit()
    {
        $this->checkToken();
        
        $member_id = $this->getUserId() ?? 0;
        $type = input('type/s', ''); // 反馈类型：bug-问题反馈，suggestion-建议，complaint-投诉
        $title = input('title/s', '');
        $content = input('content/s', '');
        $contact = input('contact/s', ''); // 联系方式
        $images = input('images/a', []); // 图片数组
        
        if (!$type || !$title || !$content) {
            return $this->renderError('请填写完整的反馈信息');
        }
        
        // 验证反馈类型
        $allowedTypes = ['bug', 'suggestion', 'complaint', 'other'];
        if (!in_array($type, $allowedTypes)) {
            return $this->renderError('反馈类型错误');
        }
        
        try {
            $feedback_data = [
                'member_id' => $member_id,
                'type' => $type,
                'title' => $title,
                'content' => $content,
                'contact' => $contact,
                'images' => !empty($images) ? json_encode($images) : '',
                'status' => 0, // 0-待处理，1-处理中，2-已处理
                'created_at' => date('Y-m-d H:i:s')
            ];
            
            $feedback_id = Db::name('feedback')->insertGetId($feedback_data);
            
            return $this->renderSuccess([
                'feedback_id' => $feedback_id,
                'message' => '反馈提交成功，我们会尽快处理'
            ]);
        } catch (Exception $e) {
            return $this->renderError('提交失败，请稍后重试');
        }
    }
    
    /**
     * 获取反馈记录
     */
    public function getList()
    {
        $this->checkToken();
        
        $member_id = $this->getUserId();
        $page = input('page/d', 1);
        $limit = input('limit/d', 10);
        $status = input('status/s', 'all'); // all-全部，pending-待处理，processing-处理中，completed-已处理
        
        $where = ['member_id' => $member_id];
        
        // 根据状态筛选
        if ($status !== 'all') {
            switch ($status) {
                case 'pending':
                    $where['status'] = 0;
                    break;
                case 'processing':
                    $where['status'] = 1;
                    break;
                case 'completed':
                    $where['status'] = 2;
                    break;
            }
        }
        
        $list = Db::name('feedback')
            ->where($where)
            ->order('created_at desc')
            ->paginate($limit, false, ['page' => $page]);
        
        // 处理数据
        foreach ($list as &$item) {
            // 处理状态文本
            switch ($item['status']) {
                case 0:
                    $item['status_text'] = '待处理';
                    break;
                case 1:
                    $item['status_text'] = '处理中';
                    break;
                case 2:
                    $item['status_text'] = '已处理';
                    break;
            }
            
            // 处理类型文本
            switch ($item['type']) {
                case 'bug':
                    $item['type_text'] = '问题反馈';
                    break;
                case 'suggestion':
                    $item['type_text'] = '建议';
                    break;
                case 'complaint':
                    $item['type_text'] = '投诉';
                    break;
                default:
                    $item['type_text'] = '其他';
                    break;
            }
            
            // 处理图片
            $item['images'] = $item['images'] ? json_decode($item['images'], true) : [];
            
            // 格式化时间
            $item['created_at_text'] = date('Y-m-d H:i', strtotime($item['created_at']));
        }
        
        return $this->renderSuccess($list);
    }
    
    /**
     * 获取反馈详情
     */
    public function getDetail()
    {
        $this->checkToken();
        
        $member_id = $this->getUserId();
        $feedback_id = input('feedback_id/d', 0);
        
        if (!$feedback_id) {
            return $this->renderError('参数错误');
        }
        
        $feedback = Db::name('feedback')
            ->where('id', $feedback_id)
            ->where('member_id', $member_id)
            ->find();
        
        if (!$feedback) {
            return $this->renderError('反馈记录不存在');
        }
        
        // 处理状态文本
        switch ($feedback['status']) {
            case 0:
                $feedback['status_text'] = '待处理';
                break;
            case 1:
                $feedback['status_text'] = '处理中';
                break;
            case 2:
                $feedback['status_text'] = '已处理';
                break;
        }
        
        // 处理类型文本
        switch ($feedback['type']) {
            case 'bug':
                $feedback['type_text'] = '问题反馈';
                break;
            case 'suggestion':
                $feedback['type_text'] = '建议';
                break;
            case 'complaint':
                $feedback['type_text'] = '投诉';
                break;
            default:
                $feedback['type_text'] = '其他';
                break;
        }
        
        // 处理图片
        $feedback['images'] = $feedback['images'] ? json_decode($feedback['images'], true) : [];
        
        return $this->renderSuccess($feedback);
    }
}